package com.itheima.controller;


import com.itheima.domain.Code;
import com.itheima.domain.Result;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice//用于标识当前类为REST风格对应的异常处理器
public class ProjectExceptionAdvice {
    //设置当前处理类对应的异常类型
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员，ex对象发送给开发人员
        return new Result(null,ex.getCode(),ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result doBussinessException(BusinessException ex){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员，ex对象发送给开发人员
        return new Result(null,ex.getCode(),ex.getMessage());
    }

    //保留对Exception类型的异常处理，用于处理非预期的异常
    @ExceptionHandler(Exception.class)
    public Result doOtherException(Exception ex){
        return new Result(null, Code.SYSTEM_UNKNOW_ERR,"系统繁忙，请稍后在试");
    }
}
